from django import forms
from django.db import transaction
from django.contrib.auth.models import User

from timesheet.models import Project

from manager.models import ProjectManagerUser


class ProjectUserForm(forms.Form):
    project_id = forms.IntegerField()
    user_id = forms.IntegerField()
    
    @transaction.commit_on_success 
    def handle(self, manager_id):
        if self.is_valid():
            project_id = self.cleaned_data['project_id']
            user_id = self.cleaned_data['user_id']
            project = Project.objects.filter(id=project_id)
            user = User.objects.filter(id=user_id)
            pmu = ProjectManagerUser.objects.filter(manager=manager_id, project=project_id, his_user=user_id)
            if(len(project)!=0 and len(user)!=0 and len(pmu)==0):
                pmu = ProjectManagerUser(manager_id=manager_id, project_id=project_id, his_user_id=user_id)
                pmu.save()
                
                            